<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 订单文件列表数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\OrderTaskFileListService;
use common\service\TenantDownloadUploadQueueService;
use support\ErrorCode;
use think\Exception;
use think\Request;
use think\Response;

class OrderTaskFileListController extends VerifyController
{

    /** @var OrderTaskFileListService */
    protected $orderTaskFileListService;
    protected $tenantDownloadUploadQueueService;

    public function __construct()
    {
        parent::__construct();
        $this->orderTaskFileListService = OrderTaskFileListService::getInstance();
        $this->tenantDownloadUploadQueueService = TenantDownloadUploadQueueService::getInstance();
    }

    /**
     * 查询上传文件列表
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function getUploadList(Request $request)
    {
        $param = $request->param()['param'];
        $taskFilter = $param['task_filter'] ?? [];
        $versionFilter = $param['version_filter'] ?? [];
        $res = $this->orderTaskFileListService->getUploadList($param, $request->getUserId(), $request->getTenantId(), $taskFilter, $versionFilter);
        return json(success_response('OK', $res));
    }

    /**
     * 查询下载文件列表
     * @param Request $request
     * @return Response
     */
    public function getDownloadList(Request $request)
    {
        $param = $request->param()['param'] ?? [];
        $taskFilter = $param['task_filter'] ?? [];
        $downloadFilter = $param['version_filter'] ?? [];
        $res = $this->orderTaskFileListService->getDownloadList($param, $request->getUserId(), $request->getTenantId(), $taskFilter, $downloadFilter);
        return json(success_response('OK', $res));
    }

    /**
     * 标记版本正在上传
     * @param Request $request
     * @return Response
     */
    public function markVersionUploading(Request $request)
    {
        $res = $this->orderTaskFileListService->markVersionUploading($request->param()['param']['filter']['version_id'], $request->getTenantId());
        return json(success_response('OK', $res));
    }

    /**
     * 标记版本已经上传
     * @param Request $request
     * @return Response
     */
    public function markVersionUploaded(Request $request)
    {
        try {
            $res = $this->orderTaskFileListService->markVersionUploaded($request->param()['data'], $request->getTenantId(), $request->getUserId());
            return json(success_response('OK', $res));
        } catch (\Throwable $e) {
            throw_strack_exception($e->getMessage(), ErrorCode::MARK_VERSION_UPLOADED_ERR);
        }
    }

    /**
     * 移除上传中标识
     * @param Request $request
     * @return Response
     */
    public function removeUploadingMark(Request $request)
    {
        $res = $this->orderTaskFileListService->removeUploadingMark($request->param()['param']['filter']['version_id_list'], $request->getTenantId());
        return json(success_response('OK', $res));
    }

    /**
     * 甲方上传列表
     * @param Request $request
     * @return Response
     */
    public function getOrderPartyAUploadList(Request $request)
    {
        $orderId = $request->param()['param']['id'];
        $page = $request->param()['param']['page'] ?? [1, C("database.database_max_select_rows")];
        $res = $this->orderTaskFileListService->getOrderPartyAUploadList($orderId, $request->getTenantId(), $page);
        return json(success_response('OK', $res));
    }

    /**
     * 甲方下载列表
     * @param Request $request
     * @return Response
     */
    public function getOrderPartyADownloadList(Request $request)
    {
        $orderId = $request->param()['param']['id'];
        $page = $request->param()['param']['page'] ?? [1, C("database.database_max_select_rows")];
        $res = $this->orderTaskFileListService->getOrderPartyADownloadList($orderId, $request->getTenantId(), $page);
        return json(success_response('OK', $res));
    }

    /**
     * 乙方上传列表
     * @param Request $request
     * @return Response
     */
    public function getOrderPartyBUploadList(Request $request)
    {
        $orderId = $request->param()['param']['id'];
        $page = $request->param()['param']['page'] ?? [1, C("database.database_max_select_rows")];
        $res = $this->orderTaskFileListService->getOrderPartyBUploadList($orderId, $request->getTenantId(), $page);
        return json(success_response('OK', $res));
    }

    /**
     * 甲方下载列表
     * @param Request $request
     * @return Response
     */
    public function getOrderPartyBDownloadList(Request $request)
    {
        $orderId = $request->param()['param']['id'];
        $page = $request->param()['param']['page'] ?? [1, C("database.database_max_select_rows")];
        try {
            $res = $this->orderTaskFileListService->getOrderPartyBDownloadList($orderId, $request->getTenantId(), $page);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 甲方标记已下载
     * @param Request $request
     * @return Response
     * @throws \think\Exception
     */
    public function partyAMarkDownloaded(Request $request)
    {
        try {
            $res = $this->orderTaskFileListService->partyAMarkDownloaded($request->param()['filter']['id']);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 标记解压成功
     * @param Request $request
     * @return Response
     */
    public function markUnzipped(Request $request)
    {
        try {
            $res = $this->orderTaskFileListService->markUnzipped($request->param()['param']['filter']['id'], $request->getUserId(), $request->getTenantId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 任务版本更新 异步检查订单是否需要追加新版本上传下载
     * @param Request $request
     * @return Response
     */
    public function asyncVersionOrderTaskUpstreamCheck(Request $request)
    {
        $this->orderTaskFileListService->asyncVersionOrderTaskUpstreamCheck($request->param()['param']['filter']['id']);
        return json(success_response('OK', null));
    }

    /**
     * 乙方下载完成 异步检查关联的下游任务能否进入就绪状态
     * @param Request $request
     * @return Response
     */
    public function asyncOrderTaskUpstreamDownloadedCheckTaskStatus(Request $request)
    {
        $this->orderTaskFileListService->asyncOrderTaskUpstreamDownloadedCheckTaskStatus($request->param()['param']['filter']['id']);
        return json(success_response('OK', null));
    }

    /**
     * 甲方项目剧集列表
     * @param Request $request
     * @return Response
     */
    public function partyAProjectEpisodeList(Request $request)
    {
        $projectId = $request->param()['param']['project_id'];
        $res = $this->orderTaskFileListService->partyAProjectEpisodeList($projectId, $request->getTenantId());
        return json(success_response('OK', $res));
    }

    /**
     * 甲方上传下载统计
     * @param Request $request
     * @return Response
     */
    public function partyACount(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->orderTaskFileListService->partyACount($param, $request->getTenantId());
        return json(success_response('OK', $res));
    }

    /**
     * 乙方上传下载统计
     * @param Request $request
     * @return Response
     */
    public function partyBCount(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->orderTaskFileListService->partyBCount($param, $request->getTenantId());
        return json(success_response('OK', $res));
    }

    /**
     * 甲方上传列表
     * @param Request $request
     * @return Response
     */
    public function getPartyAUploadList(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->orderTaskFileListService->getPartyAUploadList($param, $request->getTenantId());
        return json(success_response('OK', $res));
    }

    /**
     * 甲方上传统计
     * @param Request $request
     * @return Response
     */
    public function getPartyAUploadCount(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->orderTaskFileListService->getPartyAUploadCount($param, $request->getTenantId());
        return json(success_response('OK', $res));
    }

    /**
     * 甲方下载列表
     * @param Request $request
     * @return Response
     */
    public function getPartyADownloadList(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->orderTaskFileListService->getPartyADownloadList($param);
        return json(success_response('OK', $res));
    }

    /**
     * 甲方下载统计
     * @param Request $request
     * @return Response
     */
    public function getPartyADownloadCount(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->orderTaskFileListService->getPartyADownloadCount($param);
        return json(success_response('OK', $res));
    }

    /**
     * 乙方上传列表
     * @param Request $request
     * @return Response
     */
    public function getPartyBUploadList(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->orderTaskFileListService->getPartyBUploadList($param);
        return json(success_response('OK', $res));
    }

    /**
     * 乙方上传统计
     * @param Request $request
     * @return Response
     */
    public function getPartyBUploadCount(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->orderTaskFileListService->getPartyBUploadCount($param);
        return json(success_response('OK', $res));
    }

    /**
     * 乙方下载列表
     * @param Request $request
     * @return Response
     */
    public function getPartyBDownloadList(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->orderTaskFileListService->getPartyBDownloadList($param);
        return json(success_response('OK', $res));
    }

    /**
     * 乙方下载统计
     * @param Request $request
     * @return Response
     */
    public function getPartyBDownloadCount(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->orderTaskFileListService->getPartyBDownloadCount($param, $request->getTenantId());
        return json(success_response('OK', $res));
    }

    /**
     * 标记为正在下载中
     * @param Request $request
     * @return Response
     */
    public function markDownloading(Request $request)
    {
        $param = $request->param()['param'];
        $taskId = $param['task_id'];
        $tenantId = $request->getTenantId();
        $downloadExtraInfo = $param['download_info'] ?? [];
        $res = $this->orderTaskFileListService->markDownloading($taskId, $downloadExtraInfo, $tenantId);
        return json(success_response('OK', $res));
    }

    /**
     * 标记下载失败
     * @param Request $request
     * @return Response
     */
    public function markDownloadErr(Request $request)
    {
        $param = $request->param()['param'];
        $taskId = $param['task_id'];
        $tenantId = $request->getTenantId();
        $this->orderTaskFileListService->markDownloadErr($taskId, $tenantId);
        return json(success_response('OK'));
    }

    /**
     * 标记为正在上传中
     * @param Request $request
     * @return Response
     */
    public function markUploading(Request $request)
    {
        $param = $request->param()['param'];
        $taskId = $param['task_id'];
        $tenantId = $request->getTenantId();
        $downloadExtraInfo = $param['upload_info'] ?? [];
        $res = $this->orderTaskFileListService->markUploading($taskId, $downloadExtraInfo, $tenantId);
        return json(success_response('OK', $res));
    }

    /**
     * 标记上传失败
     * @param Request $request
     * @return Response
     */
    public function markUploadErr(Request $request)
    {
        $param = $request->param()['param'];
        $taskId = $param['task_id'];
        $tenantId = $request->getTenantId();
        $this->orderTaskFileListService->markUploadErr($taskId, $tenantId);
        return json(success_response('OK'));
    }

    /**
     * 标记上传成功
     * @param Request $request
     * @return Response
     * @throws Exception
     * @throws \Throwable
     */
    public function markUploaded(Request $request)
    {
        $data = $request->param()['data'];
        $res = $this->orderTaskFileListService->markUploaded($data, $request->getTenantId());
        return json(success_response('OK', $res));
    }
}
